package lyl.html;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;

import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;


/**
 * @author Liu Yulong
 */
public class HtmlUtil {
    
    public static void main(String[] args){
        //URL url = new URL("http://www.ucap.com.cn/aaa/bbb/ccc.jpg");
		//System.out.println(url.getFile());
		parserTest();
        
    }
    public static void parser() {
        String html = "<a href=\"/cn/architecture\" name=\"architecture\" id=\"1,390\" onclick=\"try {CategoryPopup.showPopup(this);} catch(e) {}; return false;\">Architecture</a>";
        Parser parser = Parser.createParser(html, "utf-8");
        try {
            NodeList list = parser.extractAllNodesThatMatch(new TagNameFilter("a"));
            SimpleNodeIterator iter1 = list.elements();
            while (iter1.hasMoreNodes()) {
                Tag tag = (Tag) iter1.nextNode();
                String href = tag.getAttribute("href");
                if (null != href) {
                    System.out.println(href);
                }
            }
        }
        catch (ParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void parserTest() {
        String html = "<p>aaaaaaaaaaa&nbsp;&nbsp;&nbsp;aaaaaaaaaaa</p>";
        Parser parser = Parser.createParser(html, "utf-8");
        try {
        	NodeList list = parser.extractAllNodesThatMatch(new TagNameFilter("p"));
            System.out.println(list.toHtml());
        }
        catch (ParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
